package com.atlc.preview.project.utils;


import com.atlc.preview.project.model.Examine;
import com.atlc.preview.project.model.Pages;
import com.atlc.preview.project.model.Problem;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 梁冲
 * @version 1.0
 * @email 1357042069@qq.com
 * @date 2021/11/17
 */
public class ObjectReadingUtils {

    private static final String EXAM_PROBLEM_PATH = "examine/ListPapers";

    private static final String TEST_EXAMINE_PATH = "test/examine";

    private static FileInputStream ips;

    private static FileOutputStream fos;

    private static ObjectInputStream ops;

    private static ObjectOutputStream oos;

    public static void write(List<?> list, Class<?> type, File file) throws Exception{
        if (type == Problem.class) {
            write(list, file);
        } else {
            write(list, file);
        }
    }

    public static Object read(Class<?> type) throws Exception{
        if (type == Problem.class) {
            return read(EXAM_PROBLEM_PATH);
        } else {
            return read(TEST_EXAMINE_PATH);
        }
    }

    public static void write(List<?> list ,File file) throws Exception{
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(list);
        oos.flush();
    }

    private static Object read(String path) throws Exception{
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
            loadExamineInfo(file);
        }
        ips = new FileInputStream(file);
        ops = new ObjectInputStream(ips);
        return ops.readObject();
    }

    private static void loadExamineInfo(File file) {
        List<Examine> lists = new ArrayList<>();
        lists.add(new Examine("5120206136", "张三", "1"));
        lists.add(new Examine("5120206135", "李四", "1"));
        lists.add(new Examine("5120206134", "王五", "1"));
        lists.add(new Examine("5120206133", "赵六", "1"));
        lists.add(new Examine("5120206132", "李七", "1"));
        try {
            ObjectReadingUtils.write(lists, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\L\\Documents\\Tencent Files\\1357042069\\FileRecv\\试卷样例.txt");

        Pages pages = FileAnalyseUtils.analyseTestPaper(file);
        System.out.println(pages);
    }
}
